"""
公共方法
"""
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import time
# 创建浏览器函数
def create_browser():
    """
    创建浏览器对象
    :return: 浏览器对象
    """
    browser = webdriver.Chrome()
    return browser

# 浏览器配置对象实现免登录操作


#Baes类存放公共方法和属性
class Base(object):
    def __init__(self,browser):
        """
        初始化浏览器对象
        :param browser:
        """
        self.browser = browser

    # 打开网页
    def base_OpenWeb(self,url):
        """
        :param url: 网页地址
        :return:
        """
        try:
            self.browser.get(url)
        except Exception as e:
            return f"{url}请确认网址是否正确",e

    # 窗口最大化
    def base_MaxWindow(self):
        """
        窗口最大化
        :return:
        """
        self.browser.maximize_window()

    # 关闭浏览器窗口
    def base_close(self):
        """
        关闭单个窗口
        :return:
        """
        self.browser.close()

    def base_quit(self):
        """
        关闭所有窗口
        :return:
        """
        self.browser.quit()

    # 元素定位
    def base_FindElement(self, locator, wait = 3):
        """
        显示等待定位元素
        :param locator: 定位器--元组形式(例如:("id","kw"))
        :param wait: 默认最长等待时间
        :return:
        """
        try:
            element = WebDriverWait(self.browser, wait).until(EC.presence_of_element_located(locator))
        except Exception as e:
            return "元素定位出错", e
        else:
            return element
    # 点击
    def base_click(self, locator):
        """
        点击
        :param locator: 元素定位
        :return:
        """
        self.base_FindElement(locator).click()

    # 输入
    def base_SendWords(self, locator, words):
        """
        输入
        :param locator: 元素定位
        :param words: 输入内容
        :return:
        """
        self.base_FindElement(locator).send_keys(words)

    # 获取文本值
    def base_GetText(self, locator):
        """
        获取标签文本值
        :param locator: 定位器
        :param custom_time: 元素加载等待时间
        :return: 错误提示
        """
        try:
            text_result = self.base_FindElement(locator).text
        except Exception as e:
            return "获取文本出错", e
        else:
            return text_result

    #选择下拉选项


if __name__ == '__main__':
    url = "https://cdszhmz.com/kshDp/loginpath"
    my_browser = create_browser()
    b = Base(my_browser)
    # 打开登录页
    b.base_OpenWeb(url)
    b.base_MaxWindow()
    # 输入账号
    b.base_SendWords(('id', 'userName'), 'smj')
    # 输入密码
    b.base_SendWords(('id', 'password'), '123456Xie')
    # 点击确定
    b.base_click(('xpath', '//*[@id="root"]/div/div/div/div/form/div[3]/button'))
    time.sleep(5)
    # 获取带班领导
    zbld = b.base_GetText(('xpath', '//*[@id="root"]/div/div/div[2]/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]'))
    # 获取低保人员数据
    dbry = b.base_GetText(('xpath', '//*[@id="root"]/div/div/div[2]/div[4]/div/div[3]/div[2]/div[1]/div/div[2]/div[1]/span[2]'))
    # 获取两残补贴数据
    lcbt = b.base_GetText(('xpath', '//*[@id="root"]/div/div/div[2]/div[4]/div/div[5]/div[2]/div[1]/div/div[2]/div[1]/span[2]'))
    print(f"带班领导:{zbld}, 低保人员:{dbry}, 重度残疾人补贴:{lcbt}")
    # 退出窗口
    b.base_close()














